import tool from '../tool'
import util from '_util/index'
const store = util.store
import _ from 'lodash'
import in_array from 'in_array'
// import Hproduct from './Hproduct'
import model from '_model';
import logic from '_logic';

export default {
    setVal(_this, flag = false) {
        let fname = _this.field.name;
        let op = util.getOp(_this);
        if (op == 'insert' && !flag)
            _this.field.fval = _this.field.def;
        else {
            let val = this.getVal(_this, fname);
            _this.$set(_this.field, 'fval', val);

        }
    },
    get_vo_state(_this) {
        let isinEditModal = store.get(_this, 'isinEditModal', 'edit');
        let isinOtherEdit = store.get(_this, 'isinOtherEdit', 'edit');
        let state = isinEditModal ? 'editVoModal' : (isinOtherEdit ? 'otherEditVo' : 'editVo');
        return state;
    },
    getVal(_this, fname) {
        let state = this.get_vo_state(_this);
        let val = store.get(_this, `${state}.${fname}`);
        return val;
    },
    getBindName(_this, id = null, model = null) {
        let field = _this.field.bind_name_field;
        let need_get_bind_name = _this.field.need_get_bind_name;
        if (!field) {
            return;
        }
        let isinEditModal = store.get(_this, 'isinEditModal', 'edit');

        let op = store.get(_this, isinEditModal ? 'modal_op' : 'op');
        // if (op != 'insert') {
        if (need_get_bind_name) {
            let url = tool.getApi(_this, 'Get', 'getField');
            if (util.isString(_this.val) && _this.val && _this.field.data_type == 0) {
                id = JSON.parse(_this.val);
            }
            let param = {
                id: id || _this.val,
                model: model || _this.bindModel,
                fname: field,
            };
            util.http.SR(_this, url, param, null, (rs) => {
                _this.bindNames = rs;
                _this.name_binded = true;
            });
        } else {

            let state = isinEditModal ? 'editVoModal' : 'editVo';
            let val = store.get(_this, `${state}.${field}`);
            _this.bindNames = val;
            _this.name_binded = true;
        }


        // }
    },
    showErrors(_this) {
        let errors = store.get(_this, 'voErrors', 'edit');
        let msgArr = []
        let hasError = false;
        for (let field in errors) {
            let error = errors[field]
            if (error) {
                hasError = true
                msgArr.push(error)
            }
        }
        if (hasError) {
            util.showMsg(_this, 'error', msgArr)
            return true;
        }
    },
    setSaveLoading(_this) {
        let eventVue = util.store.get(_this, 'eventVue');
        eventVue.$emit('setSaveLoading', true);
        _this.loadingBtn = true;
        setTimeout(() => {
            _this.loadingBtn = false;
            eventVue.$emit('setSaveLoading', false);
        }, 1000 * 5);
    },
    showEditPop(_this, mid, yes_event, editVoModal = {}, info_extend) {
        // store.push(_this, 'setIsinEditModal', 'isinEditModal', false, true, 'edit');
        util.store.commit(_this, 'setEditVoModal', editVoModal);

        let eventVue = util.store.get(_this, 'eventVue');
        let info = {
            mid: mid,
            yes_event: yes_event
        };
        info = _.merge(info, info_extend);
        eventVue.$emit('setModal')
        eventVue.$emit('setModalContent', info)
    },
    set_enum_val(_this, notSelect) {
        let tmp = _this.field.enum_val;
        if (util.isObject(tmp)) {
            let hl = [];
            for (let key in tmp) {
                let item = {
                    id: key,
                    title: tmp[key]
                };
                hl.push(item);
            }
            _this.enum_val = hl;
        } else {
            if (notSelect) {
                let rs = _.findIndex(tmp, (o) => {
                    return o.id && o.title
                })
                if (rs == -1) {
                    tmp = tmp.map((title, id) => {
                        return {
                            id,
                            title
                        }
                    })
                }
            }
            _this.enum_val = tmp;
        }
    },
    getSpecialTypeName(_this, type, clb) {
        let url = tool.getApi(null, 'Editget', 'getSpecialTypeName');
        let param = {type};
        util.getDataCom(_this, url, param, (rs) => {
            clb(rs)
        })
    },
    hlShowLine(_this) {
        switch (_this.field.id) {
            case 117:
                model.Hproduct.showSpecialTypeName(_this);
                break;
        }
    },
    beforeSubmit(_this, ctl) {
        switch (ctl) {
            case 'Hproduct':
                model.Hproduct.setType(_this);
                break;
        }
    },
    afterSubmit_success(_this) {
        if (_this.modal_edit) {
            logic.list.refreshListData(_this);
        }
    },
    bind_val_field(_this) {
        let field = null;
        switch (_this.ctl) {
            case 'Member':
                // switch (_this.page_ctl) {
                //     case 'Area':
                //         field = 'name';
                //         break;
                // }
                break;
        }
        _this.val_field = field;
    },
    bind_fields(_this) {
        let head = [
            'id', 'title', 'name'
        ];
        let middle = [];
        switch (_this.bindModel) {
            case 'Navigation':
                middle = [
                    'level', 'parent_id'
                ];
                break;
            case 'TrProduct':
                middle = [
                    'attr_name', 'product_num', 'supplier_name'
                ];
                break;
            case 'Qrcode':
                middle = [
                    'chn_id'
                ];
                break;
            case 'WarehouseTopProduct':
                middle = [
                    'top_product_name', 'top_product_id'
                ];
                break;
            case 'Useraccount':
                middle = [
                    'account_id', 'users_id_real', 'nick_name', 'account_type'
                ];
            case 'UserCentre':
                middle = [
                    'nickname', 'nick_name', 'mobile'
                ];
                break;
        }
        // tool.cl('_this.bindModel', _this.bindModel);
        // tool.cl('middle', middle);
        let foot = [
            'create_time', 'update_time', 'create_dt', 'update_dt'
        ];
        _this.fields = _.concat(head, middle, foot);
    },
    hl_field_type_conf(_this, ctl_field) {
        if (_this.field_type_conf && _this.field_type_conf.length > 0) {
            let conf = _this.field_type_conf.find((item) => item.ctl_field == ctl_field);
            let type_field = conf.type_field;
            let val = Number(_this._vo[ctl_field]);
            let type = conf.kv[val];
            _this.fields.forEach((field, ind) => {
                if (field.name == type_field) {
                    field.type = type;
                    // _this.$set(_this.fields, ind, field);
                }

            });
        }
    },
    hl_field_rel_conf(_this, rel_field, val) {
        if (_this.field_rel_conf && _this.field_rel_conf.length > 0) {
            let confs = _this.field_rel_conf.filter((item) => item.rel_field == rel_field);
            confs.forEach(conf => {
                let target_field = conf.target_field;
                let type = conf.type;
                conf.kv.forEach(obj => {
                    if (_.has(obj, val)) {
                        let targetVal = obj[val];

                        this._toChangeVal(_this, target_field, targetVal, type);
                    }

                });
            });

        }
    },
    toSetEditVo(_this, name, val) {

        let {vo, evnetName} = this.getEditVo(_this);
        vo[name] = val;
        util.store.commit(_this, evnetName, vo);
    },
    getEditVo(_this, name) {
        let evnetName = _this.forModal ? 'setEditVoModal' : (_this.otherEdit ? 'setOtherEditVo' : 'setEditVo');

        let voName = _this.forModal ? 'editVoModal' : (_this.otherEdit ? 'otherEditVo' : 'editVo');

        let vo = _this.$store.state.global[voName];
        return name ? vo[name] : {vo, evnetName};
    },
    _toChangeVal(_this, target_field, targetVal, type) {
        let field = _this.fields.find((item) => item.name == target_field);
        if (field) {
            switch (type) {
                case 'value':
                    // this.toSetEditVo(_this,target_field, targetVal);
                    let eventVue = util.store.get(_this, 'eventVue');
                    eventVue.$emit('editCom_toSetEditVo', target_field, targetVal);
                    util.eventEmit(_this, 'up_fval', target_field);
                    break;
                case 'value_disable':
                    util.eventEmit(_this, 'up_fval_disable', [target_field, targetVal]);
                    break;
                case 'disable':
                    util.eventEmit(_this, 'up_disable', [target_field, targetVal]);
                    break;
            }

        }
    },
    onPreSubmit(_this, _valid) {
        util.showConfirm(_this, () => {
            if (logic.edit.showErrors(_this))
                return;

            let submit_clb = util.modelFn(_this.edit_model, 'get_submit_clb', _this);
            if (_this.otherEdit) {
                let vo = util.store.get(_this, 'otherEditVo');

                if (_this.otherEdit_option && _this.otherEdit_option.can_submit) {
                    _this.toSubmit((this2, rs) => {
                        // tool.cl('submit_clb:', submit_clb);
                        if (submit_clb)
                            submit_clb(this2, rs);
                        _this.$emit('dosth', vo);
                    });
                } else {
                    _this.$emit('dosth', vo);
                }
            } else if (_this.forModal) {
                if (!_valid.modalEdit.valid(_this, _this.modal_info.mid)) {
                    return;
                }
                let willNext = _this.modal_info.willNext;
                if (!willNext) {
                    _this.$emit('toHidden');
                }
                let yes_event = _this.modal_info.yes_event
                if (util.isFunction(yes_event)) {
                    yes_event()
                } else {
                    let eventVue = util.store.get(_this, 'eventVue');
                    eventVue.$emit(yes_event)
                }


            } else {
                if (!_valid.vedit.valid(_this, _this.mid)) {
                    return;
                }

                _this.toSubmit(submit_clb)
            }
            // let eventVue = util.store.get(_this,'eventVue');
            // eventVue.$emit('setPop')
        }, {content: '确认提交吗？'});
    },
    set_edit_conf(_this) {
        let url = tool.getApi(_this, 'Editget', 'get_edit_conf');
        util.http.SR(_this, url, {mid: _this.mid}, false, (rs) => {
            if (rs) {
                let fnames = _this.fields.map(field => {
                    return field.name
                });
                _this.field_rel_conf = [];
                _this.field_type_conf = [];
                _this.showConf = [];
                if (rs.rel)
                    rs.rel.forEach(item => {
                        if (in_array(item.rel_field, fnames))
                            _this.field_rel_conf.push(item);
                    })
                if (rs.type)
                    rs.type.forEach(item => {
                        if (in_array(item.ctl_field, fnames))
                            _this.field_type_conf.push(item);
                    })
                if (rs.show)
                    rs.show.forEach(item => {
                        if (in_array(item.judge, fnames))
                            _this.showConf.push(item);
                    })

                _this.getConfed = true
            }

        });
    },
    hl_hide_type(_this) {

        let op = util.getOp(_this);

        _this.fields.forEach(field => {
            let _showType = -1;
            let showType = field.showType;
            let hide_type = field.hide_type;

            switch (op) {
                case 'insert':
                    _showType = 3;
                    break;
                case 'update':
                    _showType = 2;
                    break;
            }
            if (showType == _showType && hide_type == 1) {
                _this.$set(_this.notShows, field.name, true);
            } else
                _this.$set(_this.notShows, field.name, false);
        });
    },
    hl_showConf(_this) {
        if (_this.showConf && _this.showConf.length > 0) {
            _this.showConf.forEach(item => {
                _this.fields.forEach(field => {
                    let judge = Number(_this._vo[item.judge]);
                    if (!!judge) {
                        if (in_array(field.name, item.hideArr))
                            _this.$set(_this.notShows, field.name, true);
                        if (in_array(field.name, item.showArr)) {
                            _this.$set(_this.notShows, field.name, false);
                        }
                    } else {

                        if (in_array(field.name, item.hideArr)) {
                            _this.$set(_this.notShows, field.name, false);
                        }
                        if (in_array(field.name, item.showArr)) {
                            _this.$set(_this.notShows, field.name, true);
                        }
                    }

                })
            });
        }
    },
    get_titleType(_this) {
        let rs = '';
        let op = _this.$store.state.global.op;
        switch (op) {
            case 'update':
                rs = '更新';
                break;
            case 'insert':
                rs = '添加';
                break;
            case 'copy':
                rs = '复制';
                break;
        }
        return rs;
    },
    is_modal_edit(_this) {
        let model = util.store.getModelByCtl(_this, _this.edit_model || _this.ctl);
        return model.is_modal_edit;
    },
    is_insert_hide(_this) {
        let model = util.store.getModelByCtl(_this, _this.edit_model || _this.ctl);
        return model.insert_hide;
    },
    is_ok_jump(_this) {
        let model = util.store.getModelByCtl(_this, _this.edit_model || _this.ctl);
        return model.ok_jump;
    },
    hl_submit_btn_load(_this) {
        let eventVue = util.store.get(_this, 'eventVue');
        eventVue.$emit('setSaveLoading', true);
        setTimeout(() => {
            _this.onsubmit = false;
            eventVue.$emit('setSaveLoading', false);
        }, 1000 * 5);
    }
}
